package com.sui.bigdata.rtcadmin.controller;

import com.sui.bigdata.rtcadmin.model.vo.CommonResponse;
import com.sui.bigdata.rtcadmin.util.CheckUtils;
import com.sui.universal.client.PermissionStrategyClient;
import com.sui.universal.client.UniversalLoginClient;
import com.sui.universal.entity.Person;
import com.sui.universal.entity.ResponseEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.sui.universal.entity.Strategy;

import java.util.ArrayList;
import java.util.List;

/**
 * @author YongChen
 * @date 2019/11/6 14:41
 * @description
 * @email yong_chen@sui.com
 */
@RestController
@CrossOrigin
@RequestMapping(value = "/login")
@Slf4j
public class LoginController extends BaseController{

    @Autowired
    private UniversalLoginClient universalLoginClient;

    @Autowired
    private PermissionStrategyClient permissionStrategyClient;

    @Autowired
    private CheckUtils checkUtils;

    @RequestMapping("/in")
    public CommonResponse in(@RequestParam("username")String username,@RequestParam("password")String password){
        ResponseEntity<String>  responseEntity =  universalLoginClient.authorize(username,password);
        checkUtils.chekcLogin(responseEntity);
        return CommonResponse.Builder.buildSuccess(responseEntity.getData());
    }

    @RequestMapping("/out")
    public CommonResponse out(@RequestHeader("Authorization")String token){
        ResponseEntity<String>  responseEntity =  universalLoginClient.out(token);
        return CommonResponse.Builder.buildSuccess(responseEntity.getData());
    }

    @RequestMapping("/info")
    public CommonResponse info(@RequestHeader("Authorization")String token){
        ResponseEntity<Person>  responseEntity =  universalLoginClient.info(token);
        return CommonResponse.Builder.buildSuccess(responseEntity.getData());
    }

    @PostMapping("/strategy")
    public CommonResponse strategy(){

        List<Strategy> list = new ArrayList();
        Strategy strategy1 = new Strategy();
        strategy1.setStrategyKey("admin");
        strategy1.setStrategyName("管理员");
        list.add(strategy1);

        Strategy strategy2 = new Strategy();
        strategy2.setStrategyKey("user");
        strategy2.setStrategyName("普通用户");
        list.add(strategy2);

        ResponseEntity<String> responseEntity = permissionStrategyClient.saveStrategyBatch(list);
        return CommonResponse.Builder.buildSuccess(null);
    }



}
